package com.examination.mlib.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.examination.mlib.R;
import com.examination.mlib.constants.PatientUrl;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.interceptor.NetWorkUtils;
import com.yilijk.base.utils.DevicesUtils;
import com.yilijk.base.utils.ScreenUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class WaterMaskView extends View {
    public WaterMaskView(Context context) {
        super(context);
    }

    public WaterMaskView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public WaterMaskView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public WaterMaskView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    Path path = new Path();
    Paint paint = new Paint();
    int rows = 0;
    int cols = 4;
    int colWidth = 0;
    String emailName = "";

    {
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(getResources().getColor(R.color.water_text));
        paint.setAlpha(120);
        paint.setStrokeWidth(10);
        paint.setTextSize(ScreenUtils.dip2px(getContext(),16));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (TextUtils.isEmpty(emailName)) {
            return;
        }
        colWidth = getWidth() / cols;
        rows = getHeight() / colWidth + 1;

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < 5; col++) {
                path.moveTo(colWidth * col, colWidth * row);
                path.lineTo(colWidth + colWidth * col, colWidth * row - colWidth);

                canvas.drawTextOnPath(emailName, path, 0, 0, paint);
                canvas.drawPath(path, paint);
                path.reset();
            }
        }
    }

//    @Override
//    public void onWindowSystemUiVisibilityChanged(int visible) {
//        super.onWindowSystemUiVisibilityChanged(visible);
//        ALog.e("oakkk", "onWindowSystemUiVisibilityChanged: " + visible);
//    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

//        setOnSystemUiVisibilityChangeListener(new OnSystemUiVisibilityChangeListener() {
//            @Override
//            public void onSystemUiVisibilityChange(int visibility) {
//                ALog.e("oakkk", "onSystemUiVisibilityChange： " + visibility);
//            }
//        });

//        NetWorkUtils.get(DevicesUtils.getCurrentUrl() + AllStringConstants.getEmployeeAccount, new NetWorkCallBack() {
//            @Override
//            public void onSuccess(String resultMsg, int code) {
//                ALog.e("oakkk", "get employee account: " + resultMsg + " " + code);
//                if (code == 200) {
//                    try {
//                        JSONObject jsonObject = new JSONObject(resultMsg);
//                        if (jsonObject.has("data")) {
//                            JSONObject dataJson = jsonObject.getJSONObject("data");
//                            if (dataJson.has("email")) {
//                                String email = dataJson.getString("email");
//                                if (!TextUtils.isEmpty(email) && email.indexOf("@") > 0) {
//                                    emailName = email.split("@")[0];
//                                }
//                            }
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//
//            @Override
//            public void onFailure(String failureMsg) {
//                ALog.e("oakkk", "get employee account error: " + failureMsg + " ");
//
//            }
//
//            @Override
//            public void onException(Exception exceptionMsg, int code) {
//                ALog.e("oakkk", "get employee account exception: " + exceptionMsg.getMessage() + " ");
//
//            }
//        });
    }

    public void refreshEmail(String emailName){
        this.emailName = emailName;
        postInvalidate();
    }

    public void getEmployeeAccount() {
        HttpUtils.get(PatientUrl.getEmployeeAccount, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s, int i) {
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.has("data")) {
                        JSONObject dataJson = jsonObject.getJSONObject("data");
                        if (dataJson.has("email")) {
                            String email = dataJson.getString("email");
                            if (!TextUtils.isEmpty(email) && email.indexOf("@") > 0) {
                                emailName = email.split("@")[0];
                            }
                        }
                    }

                    postInvalidate();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String s, int i) {

                postInvalidate();
            }
        });
    }
}
